package cn.ittaoli.ajax.controller;

import cn.ittaoli.ajax.model.*;
import cn.ittaoli.ajax.service.ArticleService;
import cn.ittaoli.ajax.service.NavService;
import cn.ittaoli.ajax.utils.FileUpload;
import cn.ittaoli.ajax.utils.JsonUtils;
import com.github.pagehelper.PageInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lub_lenovo on 2017/8/6.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private NavService navService;
    @Autowired
    private ArticleService articleService;


    @RequestMapping("/index")
    public String index(){
        return "article/index";
    }



    @RequestMapping("/kind")
    public String kind(){
        return "article/kindeditor";
    }

    @RequestMapping("/ke")
    public String ke(){
        return "article/ke-textarea";
    }
    @RequestMapping("/kem")
    public String kem(){
        return "article/ke-multiimage";
    }

//    @RequestMapping(value = "/upload", produces = { "application/json;charset=UTF-8" })
    @RequestMapping(value = "/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException {
        String path= FileUpload.upload(file,request);

        Map<String, Object> result = new HashMap<String,Object>();
        result.put("error", 0);
        result.put("message", "上传成功");
        result.put("url",path);
//        为了保证功能的兼容性，要把result的对象转换成json的字符串
        return JsonUtils.objectToJson(result);
//        return result;
    }

    @RequestMapping("/articleList")
    public String articleList(Model model, @ModelAttribute("msg") String msg,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5")Integer rowsPerPage){
        model.addAttribute("msg",msg);
        model.addAttribute("menu","文章列表");
        //分页参数，从那一条开始取，一般传递的是第几页,page
        //1页，0,5
        //2页，5,5
        //3页，10,5
//        Integer start=(page-1)*rowPerPage;
        //每页有5条数据,rowPerPage

        PageInfo<ArticleView> pageInfo=articleService.getByPage(page,rowsPerPage);
        model.addAttribute("pageInfo",pageInfo);
        return "article/articleList";
    }


    @RequestMapping("/articleAdd")
    public String articleAdd(Model model){
        List<Nav> nav = navService.getAll();
        model.addAttribute("nav",nav);
        model.addAttribute("menu","新增文章");
        return "article/articleAdd";
    }

    @RequestMapping("/insertArticle")
    public String insertArticle(ArticleFull articleFull, RedirectAttributes attr){
        int res = articleService.insertArticle(articleFull);
        if(res>0){
            attr.addAttribute("msg","添加成功");
            return "redirect:articleList.do";
        }else{
            return "article/articleAdd";
        }
    }



}
